package com.cooba.activitys.user;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cooba.R;
import com.cooba.activitys.MainActivity;
import com.cooba.utils.MySqlOpenHelper;
/**
 * Created by Administrator on 2015/12/16.
 * 用户信息
 */
public class UserInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView mChangename, mGet_changenumber;
    private EditText mEt_changename;
    private ImageView mLoginimage_back;
    private RelativeLayout mChangPassword;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        mChangename = (TextView) findViewById(R.id.changename);
        mLoginimage_back = (ImageView) findViewById(R.id.loginimage_back);
        mGet_changenumber = (TextView) findViewById(R.id.get_changenumber);
        mEt_changename = (EditText) findViewById(R.id.et_changename);
        mChangPassword= (RelativeLayout) findViewById(R.id.changPassword);
        mChangename.setOnClickListener(this);
        mLoginimage_back.setOnClickListener(this);
        mChangPassword.setOnClickListener(this);
        mEt_changename.setOnClickListener(this);
        getCont();
    }


    //读取数据
    private void getCont() {
        MySqlOpenHelper helper = new MySqlOpenHelper(getApplication());
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.query("login_date", null, null, null, null, null, null, null);
        if (cursor.moveToFirst()) {
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String ph = cursor.getString(cursor.getColumnIndex("phone"));
            if (ph.equals("") || name.equals("")) {
                mChangename.setText("");
            } else {
                mChangename.setText(name);
                String phone = ph.substring(0, 3) + "****" + ph.substring(7, ph.length());
                mGet_changenumber.setText(phone);
            }
        }
        cursor.close();
        db.close();
    }

    @Override
    public void onClick(View v) {
        Intent intent;

        switch (v.getId()) {
            case R.id.changename:
                mEt_changename.setVisibility(View.VISIBLE);
                mChangename.setVisibility(View.GONE);
                mEt_changename.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus) {

                        } else {
                            String name = mEt_changename.getText().toString();
                            mChangename.setText(name);
                            mEt_changename.setVisibility(View.GONE);
                            mChangename.setVisibility(View.VISIBLE);
                        }
                    }
                });

                break;
            case R.id.loginimage_back:
                intent = new Intent(getApplication(), MainActivity.class);
                startActivity(intent);
                finish();
                break;
            //修改密码
            case R.id.changPassword:
                intent = new Intent(getApplication(), ChangPasswordActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    //屏幕监听
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            Log.v("Himi", "ACTION_DOWN");
        } else if (event.getAction() == MotionEvent.ACTION_UP) {
            Log.v("Himi", "ACTION_UP");
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            mEt_changename.clearFocus();
            if (mEt_changename.getText().length() > 0) {
            } else {
                mEt_changename.setVisibility(View.VISIBLE);
            }
            Log.v("Himi", "ACTION_MOVE");
        }
        return true;

    }
}
